﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataAnalyticsTools.Models
{
    /// <summary>
    /// 趋势信息
    /// </summary>
    public class FeatureTrendInfo
    {
        /// <summary>
        /// 特征索引
        /// </summary>
        public int FeatureIndex { get; set; }

        /// <summary>
        /// 特征名称
        /// </summary>
        public string FeatureName { get; set; }

        /// <summary>
        /// 趋势斜率
        /// </summary>
        public float Slope { get; set; }

        /// <summary>
        /// 趋势方向
        /// </summary>
        public string Direction => Slope > 0 ? "上升" : Slope < 0 ? "下降" : "平稳";

        /// <summary>
        /// 趋势强度
        /// </summary>
        public float Strength { get; set; }

        /// <summary>
        /// 趋势显著性（p值）
        /// </summary>
        public float Significance { get; set; }

        /// <summary>
        /// 趋势描述
        /// </summary>
        public string Description => $"{FeatureName}: {Direction}趋势(强度: {Strength:P0}, 斜率: {Slope:F3})";

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="featureIndex">特征索引</param>
        /// <param name="slope">趋势斜率</param>
        /// <param name="strength">趋势强度</param>
        /// <param name="significance">趋势显著性</param>
        public FeatureTrendInfo(int featureIndex, float slope, float strength, float significance = 0.05f)
        {
            FeatureIndex = featureIndex;
            FeatureName = $"特征{featureIndex}";
            Slope = slope;
            Strength = strength;
            Significance = significance;
        }

        /// <summary>
        /// 带特征名称的构造函数
        /// </summary>
        /// <param name="featureIndex">特征索引</param>
        /// <param name="featureName">特征名称</param>
        /// <param name="slope">趋势斜率</param>
        /// <param name="strength">趋势强度</param>
        /// <param name="significance">趋势显著性</param>
        public FeatureTrendInfo(int featureIndex, string featureName, float slope, float strength, float significance = 0.05f)
        {
            FeatureIndex = featureIndex;
            FeatureName = featureName ?? $"特征{featureIndex}";
            Slope = slope;
            Strength = strength;
            Significance = significance;
        }

        /// <summary>
        /// 获取趋势等级
        /// </summary>
        public string TrendLevel
        {
            get
            {
                var absoluteSlope = Math.Abs(Slope);
                if (absoluteSlope > 0.1) return "强烈";
                if (absoluteSlope > 0.05) return "中等";
                if (absoluteSlope > 0.01) return "轻微";
                return "无趋势";
            }
        }

        /// <summary>
        /// 是否显著
        /// </summary>
        public bool IsSignificant => Significance < 0.05f;

        /// <summary>
        /// 转换为字符串表示
        /// </summary>
        public override string ToString()
        {
            var significanceText = IsSignificant ? "显著" : "不显著";
            return $"{FeatureName}: {Direction}({TrendLevel}), 斜率:{Slope:F4}, 强度:{Strength:P0}, {significanceText}";
        }
    }
}
